package com.example.zp.sqldata;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.zp.liaotian.Address_ListData;
import com.example.zp.liaotian.Buddy_ListData;
import com.example.zp.liaotian.ChatMsgEntity;

import java.util.ArrayList;

/**
 * Created by donar_work on 2016/11/9.
 */

public class MySqliteUtlis {
    private final MyMySQLiteHelper mysqlis;
    private Context context;
    private SQLiteDatabase db;
    private static final String BUDDY="BuddyList";
    private static final String CHAT="Chat";
    private static final String RECENT = "Recent";
    private static final String ADDRESS = "Rddress";

    private MySqliteUtlis(Context context) {
        this.context = context;
       mysqlis= new MyMySQLiteHelper(context);
        if (mysqlis!=null)
          db=  mysqlis.getWritableDatabase();
    }
    public static MySqliteUtlis GetInstan(Context context){

        if (mySqliteUtlis == null){
           mySqliteUtlis=  new MySqliteUtlis(context);
        }
        return mySqliteUtlis;
    }
    private static MySqliteUtlis mySqliteUtlis = null;

//存储数据，存储最近浏览设备
//    public void Storingdata(String Name, String DeviceID, String PTZType, String DeviceType, String ParentID, String BrowseTime){
//         db.execSQL("insert into "+SQUSER+" (Name,DeviceID,PTZType,DeviceType,ParentID,BrowseTime) values (?,?,?,?,?,?)",new String[]{Name,DeviceID,PTZType,DeviceType,ParentID,BrowseTime});
//    }
    //存储好友列表数据
    public void StoringBuddydata(int m_account_id){
         db.execSQL("insert into "+BUDDY+" (m_account_id) values (?)",new Object[]{m_account_id});
    }
    //存储聊天记录数据
    public void StoringChatData(int to_id , String text , int local_id, String Date, String Name, boolean isSelf, int flag){
        db.execSQL("insert into "+CHAT+" (to_id,Msg,local_id,Date,Name,isSelf,flag) values (?,?,?,?,?,?,?,?)",new Object[]{to_id,text,local_id,Date,Name,isSelf,flag});
    }
    //存储最近联系人
    public void StoringRecentData(int send_id , String text , String Date, String Name, String Unread){
        db.execSQL("insert into "+RECENT+" (send_id,Msg,Date,Name,Unread) values (?,?,?,?,?)",new Object[]{send_id,text,Date,Name,Unread});
    }
    //存储地址
    public void StoringAddressData(int caddr_id , String contats , String address , String mobile){
        db.execSQL("insert into "+ADDRESS+" (caddr_id,contats,address,mobile) values (?,?,?,?)",new Object[]{caddr_id ,contats,address,mobile});
    }
    //查找聊天数据
    public ArrayList<ChatMsgEntity> showChatData(int to_id, int local_id){
        ArrayList<ChatMsgEntity> list = new ArrayList<>();
        ChatMsgEntity chatMsgEntity = null;
        Cursor cursor = db.rawQuery("select * from "+CHAT+" where to_id in(?,?) and local_id in(?,?)", new String[]{String.valueOf(to_id), String.valueOf(local_id), String.valueOf(to_id), String.valueOf(local_id)});
        cursor.moveToPrevious();
        while (cursor.moveToNext()){
            chatMsgEntity = new ChatMsgEntity();
            chatMsgEntity.setTo_id(cursor.getInt(cursor.getColumnIndex("to_id")));
            chatMsgEntity.setLocal_id(cursor.getInt(cursor.getColumnIndex("local_id")));
            chatMsgEntity.setText(cursor.getString(cursor.getColumnIndex("Msg")));
            chatMsgEntity.setDate(cursor.getString(cursor.getColumnIndex("Date")));
            chatMsgEntity.setName(cursor.getString(cursor.getColumnIndex("Name")));
            chatMsgEntity.setSelf(Boolean.getBoolean(cursor.getString(cursor.getColumnIndex("isSelf"))));
            chatMsgEntity.setFlag(cursor.getInt(cursor.getColumnIndex("flag")));
            list.add(chatMsgEntity);
        }
        return list;
    }

    //查找所有地址信息数据
    public ArrayList<Address_ListData> showaddressdata(){
        ArrayList<Address_ListData> list=new ArrayList<>();
        Address_ListData address_ListData = null;
        Cursor cursor = db.rawQuery("select * from "+ADDRESS+" order by id asc", null);//desc:降序，asc：升序
        cursor.moveToPrevious();
        //获取name列的索引
        while (cursor.moveToNext()) {
            address_ListData = new Address_ListData();
            address_ListData.setCaddr_id(cursor.getInt(cursor.getColumnIndex("caddr_id")));
            address_ListData.setContats(cursor.getString(cursor.getColumnIndex("contats")));
            address_ListData.setAddress(cursor.getString(cursor.getColumnIndex("address")));
            address_ListData.setMobile(cursor.getString(cursor.getColumnIndex("mobile")));
            list.add(address_ListData);
        }
//        cursor.close();//关闭结果集
//        db.close();//关闭数据库对象
        return list;
    }
    //查找所有数据，返回所有ID
    public ArrayList<String> showaAddessid(){
        ArrayList<String> list=new ArrayList<>();
        Cursor cursor = db.rawQuery("select * from "+ADDRESS+" order by id asc", null);
        cursor.moveToPrevious();
        //获取name列的索引
        while (cursor.moveToNext()) {
            String st = cursor.getString(cursor.getColumnIndex("caddr_id"));
            list.add(st);
        }
//        cursor.close();//关闭结果集
//        db.close();//关闭数据库对象
        return list;

    }
//查找所有好友数据
    public ArrayList<Buddy_ListData> showdata(){
        ArrayList<Buddy_ListData> list=new ArrayList<>();
        Buddy_ListData buddy_listData = null;
        Cursor cursor = db.rawQuery("select * from "+BUDDY+" order by id asc", null);//desc:降序，asc：升序
        cursor.moveToPrevious();
        //获取name列的索引
        while (cursor.moveToNext()) {
            buddy_listData = new Buddy_ListData();
            buddy_listData.setM_account_id(cursor.getInt(cursor.getColumnIndex("m_account_id")));
            list.add(buddy_listData);
        }
//        cursor.close();//关闭结果集
//        db.close();//关闭数据库对象
        return list;

    }
    //查找好友ID
    public ArrayList<Object> showalldata(){
        ArrayList<Object> list=new ArrayList<>();
        Buddy_ListData buddy_listdata = null;
        Cursor cursor = db.rawQuery("select * from "+BUDDY, null);
        cursor.moveToPrevious();
        //获取name列的索引
        while (cursor.moveToNext()) {
            int in = cursor.getInt(cursor.getColumnIndex("m_account_id"));
//            buddy_listdata = new Buddy_ListData();
//            buddy_listdata.setM_account_id(cursor.getColumnIndex("m_account_id"));
//            data_eqment.setM_account_id(cursor.getString(cursor.getColumnIndex("DeviceID")));

            list.add(in);
        }
//        cursor.close();//关闭结果集
//        db.close();//关闭数据库对象
        return list;

    }
    //根据ID查找在线状态
   /* public String showdatadevstate(String DeviceID){
        String Online ="";
        Cursor cursor = db.rawQuery("select * from "+ALLSQUSER+" where DeviceID=?", new String[]{DeviceID} );
        cursor.moveToPrevious();
        //获取name列的索引
        while (cursor.moveToNext()) {
            Online = cursor.getString(cursor.getColumnIndex("Online"));
//            list.add(data_eqment);
//        cursor.close();//关闭结果集
//        db.close();//关闭数据库对象
        }
        return Online;
    }

    //根据PID查找设备
    public ArrayList<Data_eqment> showdataParentID(String ParentID){
        ArrayList<Data_eqment> list=new ArrayList<>();
        Data_eqment data_eqment = null;
        Cursor cursor = db.rawQuery("select * from "+ALLSQUSER+" where ParentID=?", new String[]{ParentID} );
        cursor.moveToPrevious();
        //获取name列的索引
        while (cursor.moveToNext()) {
            data_eqment = new Data_eqment();
            data_eqment.setDeviceID(cursor.getString(cursor.getColumnIndex("DeviceID")));
            data_eqment.setName(cursor.getString(cursor.getColumnIndex("Name")));
            data_eqment.setPTZType(cursor.getString(cursor.getColumnIndex("PTZType")));
            data_eqment.setDeviceType(cursor.getString(cursor.getColumnIndex("DeviceType")));
            data_eqment.setParentID(cursor.getString(cursor.getColumnIndex("ParentID")));
            data_eqment.setOnline(cursor.getString(cursor.getColumnIndex("Online")));
            list.add(data_eqment);
//            list.add(data_eqment);
//        cursor.close();//关闭结果集
//        db.close();//关闭数据库对象
        }
        return list;
    }
    public Data_eqment showdatadev(){
//        ArrayList<Data_eqment>list=new ArrayList<>();
        Data_eqment data_eqment = null;
        Cursor cursor = db.rawQuery("select * from "+SQUSER+" order by id desc", null );
        cursor.moveToPrevious();
        //获取name列的索引
        while (cursor.moveToNext()) {
            data_eqment = new Data_eqment();
            data_eqment.setDeviceID(cursor.getString(cursor.getColumnIndex("DeviceID")));
            data_eqment.setName(cursor.getString(cursor.getColumnIndex("Name")));
//            list.add(data_eqment);
        }
//        cursor.close();//关闭结果集
//        db.close();//关闭数据库对象
        return data_eqment;
    }
//查找最近浏览数据，返回最近浏览DeviceID
    public ArrayList<String> showdatast(){
        ArrayList<String> list=new ArrayList<>();
        Cursor cursor = db.rawQuery("select * from "+SQUSER+" order by id desc", null);
        cursor.moveToPrevious();
        //获取name列的索引
        while (cursor.moveToNext()) {
            String st = cursor.getString(cursor.getColumnIndex("DeviceID"));
            list.add(st);
        }
//        cursor.close();//关闭结果集
//        db.close();//关闭数据库对象
        return list;

    }
    //查找所有数据，返回所有DeviceID
    public ArrayList<String> showalldatast(){
        ArrayList<String> list=new ArrayList<>();
        Cursor cursor = db.rawQuery("select * from "+ALLSQUSER+" order by id desc", null);
        cursor.moveToPrevious();
        //获取name列的索引
        while (cursor.moveToNext()) {
            String st = cursor.getString(cursor.getColumnIndex("DeviceID"));
            list.add(st);
        }
//        cursor.close();//关闭结果集
//        db.close();//关闭数据库对象
        return list;

    }
    //查找所有数据，返回所有Name
    public ArrayList<String> showalldevname(){
        ArrayList<String> list=new ArrayList<>();
        Cursor cursor = db.rawQuery("select * from "+ALLSQUSER+" order by id desc", null);
        cursor.moveToPrevious();
        //获取name列的索引
        while (cursor.moveToNext()) {
            String st = cursor.getString(cursor.getColumnIndex("Name"));
            list.add(st);
        }
//        cursor.close();//关闭结果集
//        db.close();//关闭数据库对象
        return list;
    }
    //
    public ArrayList<Data_eqment> showalldataname(){
        ArrayList<Data_eqment> list=new ArrayList<>();
        Data_eqment data_eqment = null;
        Cursor cursor = db.rawQuery("select * from "+ALLSQUSER, null);
        cursor.moveToPrevious();
        //获取name列的索引
        while (cursor.moveToNext()) {
            data_eqment = new Data_eqment();
            data_eqment.setName(cursor.getString(cursor.getColumnIndex("Name")));
            list.add(data_eqment);
        }
//        cursor.close();//关闭结果集
//        db.close();//关闭数据库对象
        return list;

    }
    // 删除最近浏览中的数据
    public void deletedata(String DeviceID) {
        db.execSQL("delete from "+SQUSER+" where DeviceID=?", new String[] { DeviceID });
    }
    // 删除所有设备中的数据
    public void deletealldata(String DeviceID) {
        db.execSQL("delete from "+ALLSQUSER+" where DeviceID=?", new String[] { DeviceID });
    }


    //Alarm_Info存储所有数据
    public void StoringAlarmdata(String DeviceID, String AlarmPriority, String AlarmTime, String AlarmDescription){
        db.execSQL("insert into "+ALARMINFO+" (DeviceID,AlarmPriority,AlarmTime,AlarmDescription) values (?,?,?,?)",new String[]{DeviceID,AlarmPriority,AlarmTime,AlarmDescription});
    }

    //Alarm_Info查找所有数据，返回所有DeviceID
    public ArrayList<String> showalarmdevid(){
        ArrayList<String> list=new ArrayList<>();
        Cursor cursor = db.rawQuery("select * from "+ALARMINFO+" order by id desc", null);
        cursor.moveToPrevious();
        //获取name列的索引
        while (cursor.moveToNext()) {
            String st = cursor.getString(cursor.getColumnIndex("DeviceID"));
            list.add(st);
        }
//        cursor.close();//关闭结果集
//        db.close();//关闭数据库对象
        return list;
    }
    //Alarm_Info查找所有数据，返回所有DeviceID
    public ArrayList<String> showalarmtime(){
        ArrayList<String> list=new ArrayList<>();
        Cursor cursor = db.rawQuery("select * from "+ALARMINFO+" order by id desc", null);
        cursor.moveToPrevious();
        //获取name列的索引
        while (cursor.moveToNext()) {
            String st = cursor.getString(cursor.getColumnIndex("AlarmTime"));
            list.add(st);
        }
//        cursor.close();//关闭结果集
//        db.close();//关闭数据库对象
        return list;
    }
    //Alarm_Info查找所有数据
    public ArrayList<Data_Alarm_Info> showalarminfo(){
        ArrayList<Data_Alarm_Info> list=new ArrayList<>();
        Data_Alarm_Info data_alarm_info = null;
        Cursor cursor = db.rawQuery("select * from "+ALARMINFO, null);
        cursor.moveToPrevious();
        //获取name列的索引
        while (cursor.moveToNext()) {
            data_alarm_info = new Data_Alarm_Info();
            data_alarm_info.setDeviceID(cursor.getString(cursor.getColumnIndex("DeviceID")));
            data_alarm_info.setAlarmDescription(cursor.getString(cursor.getColumnIndex("AlarmDescription")));
//            data_alarm_info.setAlarmMethod(cursor.getString(cursor.getColumnIndex("AlarmMethod")));
            data_alarm_info.setAlarmPriority(cursor.getString(cursor.getColumnIndex("AlarmPriority")));
            data_alarm_info.setAlarmTime(cursor.getString(cursor.getColumnIndex("AlarmTime")));
            list.add(data_alarm_info);
        }
//        cursor.close();//关闭结果集
//        db.close();//关闭数据库对象
        return list;

    }
    public void deleteAlarmdata(String DeviceID, String AlarmTime) {
        db.execSQL("delete from "+ALARMINFO+" where DeviceID=? and AlarmTime=?", new String[] { DeviceID,AlarmTime});
    }*/
}
